<template>
  <div class="add-container">
    <el-form
      :model="articleRuleForm"
      :rules="articleRules"
      ref="addRef"
      label-width="100px"
      class="demo-ruleForm"
    >
      <el-form-item label="文章标题" prop="title">
        <el-input v-model="articleRuleForm.title" width="380px"></el-input>
      </el-form-item>
      <el-form-item label="文章内容" prop="articleBody">
        <quill-editor
          v-model="articleRuleForm.articleBody"
          class="add-quill"
          :options="addOptions"
        >
        </quill-editor>
      </el-form-item>
      <el-form-item label="视频地址" prop="address">
        <el-input v-model="articleRuleForm.address" width="380px"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="onAddCanel">取 消</el-button>
      <el-button type="primary" @click="onAddFn">确 定</el-button>
    </span>
  </div>
</template>

<script>
import { add } from "@/api/hmmm/articles.js";
export default {
  name: "ArticlesAdd",
  data() {
    return {
      articleRuleForm: {
        title: "",
        articleBody: "",
        videoURL: "",
      },
      articleRules: {
        title: [
          { required: true, message: "请输入文章标题", trigger: "blur" },
          {
            min: 3,
            max: 15,
            message: "长度在 3 到 15 个字符",
            trigger: "blur",
          },
        ],
        articleBody: [
          { required: true, message: "请输入文章内容", trigger: "blur" },
        ],
      },
      addOptions: {
        modules: {
          toolbar: [
            ["bold", "italic", "underline", "strike"],
            [{ list: "ordered" }, { list: "bullet" }],
            ["blockquote"],
            ["code-block", "image", "link"],
          ],
        },
      },
    };
  },
  methods: {
    onAddFn() {
      this.$refs.addRef.validate(async (valid) => {
        if (!valid) {
          this.$message.error("请输入必填项");
        } else {
          await add(this.articleRuleForm);
          this.$message.success("添加成功");
          this.$emit("update-list");
        }
      });
    },
    onAddCanel() {
      this.$emit('update-add')
      this.$refs.addRef.resetFields();
    },
  },
};
</script>

<style scoped lang='less'>
.add-container {
  /deep/.add-quill {
    width: 530px;
    height: 200px;
    margin-bottom: 55px !important;
  }
  /deep/ .el-input__inner {
    width: 530px;
  }
  /deep/.dialog-footer {
    display: flex;
    justify-content: center;
  }
}
</style>
